﻿using System;
using RaytracerLib.MathLib;
using System.Collections.Generic;
using System.Threading;
using System.Drawing;

namespace RaytracerLib.Samplers
{
    public class JitterSampler : UniformSampler
    {
        private Random m_random;

        internal override void RenderStart(RenderStartPhase a_phase)
        {
            base.RenderStart(a_phase);

            if (a_phase == RenderStartPhase.PrepareObjectToRender)
                m_random = new Random(Film.Scene.RenderOptions.RandomSeed);
        }

        public override IEnumerable<Vector2> GetSamples(Rectangle a_rect)
        {
            int samples_x = a_rect.Width * Subresolution;
            int samples_y = a_rect.Height * Subresolution;

            for (int y = 0; y < samples_y; y++)
            {
                for (int x = 0; x < samples_x; x++)
                {
                    lock (m_random)
                    {
                        yield return new Vector2(
                            a_rect.Left + (x + m_random.NextDouble()) / Subresolution,
                            a_rect.Top + (y + m_random.NextDouble()) / Subresolution);
                    }
                }
            }
        }

        public override SamplerType SamplerType
        {
            get
            {
                return SamplerType.Jitter;
            }
        }
    }
}